package cqie.frame.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cqie.frame.entity.Clazz;
import cqie.frame.entity.ClazzDetail;
import cqie.frame.entity.qo.ClazzQo;
import cqie.frame.entity.vo.ClazzVo;
import cqie.frame.response.CodeMsg;
import cqie.frame.response.ResultJson;
import cqie.frame.service.ClazzDetailService;
import cqie.frame.service.IClazzService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Clazz)表控制层
 *
 * @author makejava
 * @since 2021-09-27 20:37:31
 */
@RestController
@RequestMapping("/clazz")
public class ClazzController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private IClazzService iClazzService;
    @Resource
    private ClazzDetailService clazzDetailService;

    //新增班级
    @ApiOperation(value = "新增班级", notes = "新增班级，同时更新班级表。", response = Clazz.class)
    @PostMapping(value = "/addClazz")
    public ResultJson addClazz(@RequestBody Clazz po) {
        try {
            //构造mo
            Clazz clazz = new Clazz();
            //复制po的属性到emp,第三个参数可选，为忽略某些属性
            BeanUtils.copyProperties(po, clazz);
            //插入用户表后回显插入班级的主键id
            iClazzService.save(clazz);
            po.setId(clazz.getId());
            //返回班级
            return ResultJson.success(po);
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //删除选中班级，多选
    @ApiOperation(value = "删除选中班级", notes = "删除选中班级。")
    @PostMapping(value = "/deleteCheckClazz")
    public ResultJson deleteCheckClazz(@RequestBody Long[] clazzIdList) {
        try {
            for (int i = 0; i < clazzIdList.length; i++) {
                iClazzService.removeById(clazzIdList[i]);
            }
            return ResultJson.success("删除成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //删除班级
    @ApiOperation(value = "删除单个班级", notes = "删除单个班级。")
    @GetMapping(value = "/deleteClazz")
    public ResultJson deleteClazz(long id) {
        try {
            //删除班级表
            iClazzService.removeById(id);
            //返回前端
            return ResultJson.success("删除成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }


    //修改班级
    @ApiOperation(value = "修改班级", notes = "修改班级，同时更新班级表。")
    @PostMapping(value = "/updateClazz")
    public ResultJson updateClazz(@RequestBody Clazz po) {
        try {
            //获取修改前的值
            Clazz clazz = new Clazz();
            //复制po的属性到sysUser,第三个参数可选，为忽略某些属性
            BeanUtils.copyProperties(po, clazz);
            //修改员工
            iClazzService.updateById(clazz);
            return ResultJson.success("修改成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "分页列表", response = ClazzVo.class)
    @PostMapping(value = "/listPaged")
    public ResultJson listPaged(@RequestBody ClazzQo qo) {
        try {
            Page<Clazz> page = new Page<>(qo.getPageIndex(), qo.getPageSize());
            //1.查询条件
            QueryWrapper<Clazz> qw = new QueryWrapper<>();
            if (!StringUtils.isEmpty(qo.getName())) {
                //查询模糊查询
                qw.like("name", qo.getName());
            }
            if (!StringUtils.isEmpty(qo.getCode())) {
                //查询模糊查询
                qw.like("code", qo.getCode());
            }
            //是否查询状态
            if (qo.getStatus() != null) {
                //不为空，启用查询
                qw.eq("status", qo.getStatus());
            }
            //2.执行查询
            IPage<Clazz> datas = iClazzService.page(page, qw);
            //3.生成返回值
            //映射
            List<ClazzVo> list = new ArrayList<>();
            IPage<ClazzVo> ret = new Page<>();
            BeanUtils.copyProperties(datas, ret);
            for (Clazz mo : datas.getRecords()) {
                ClazzVo vo = new ClazzVo();
                BeanUtils.copyProperties(mo, vo);
                //映射--状态
                vo.setStatusName(mo.getStatus() == 0 ? "正常" : "禁用");
                list.add(vo);
            }
            ret.setRecords(list);
            return ResultJson.success(ret);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "查看详情", response = ClazzVo.class)
    @GetMapping(value = "/details")
    public ResultJson details(Long id) {
        try {
            //1.获取mo
            Clazz mo = iClazzService.getById(id);
            //2.映射成vo
            ClazzVo vo = new ClazzVo();
            BeanUtils.copyProperties(mo, vo);
            //映射准备
            //映射--状态
            vo.setStatusName(mo.getStatus() == 0 ? "正常" : "禁用");
            //3.返回
            return ResultJson.success(vo);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "班级详情页面", response = ClazzVo.class)
    @GetMapping(value = "/clazzDetails")
    public ResultJson clazzDetails(Long id) {
        try {
            //1.获取mo
            Clazz mo = iClazzService.getById(id);
            //2.映射成vo
            ClazzVo vo = new ClazzVo();
            BeanUtils.copyProperties(mo, vo);
            //映射准备
            //映射--状态
            vo.setStatusName(mo.getStatus() == 0 ? "正常" : "禁用");
            //3.返回
            //映射准备
            List<ClazzDetail> clazzDetailList = new ArrayList<>();
            QueryWrapper<ClazzDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",id);
            clazzDetailList = clazzDetailService.list(queryWrapper);

            Map<String,Object> map = new HashMap<>();
            map.put("clazz", vo);
            map.put("clazzDetailList", clazzDetailList);
            return ResultJson.success(map);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //注销/恢复选中班级，多选
    @ApiOperation(value = "注销/恢复选中班级", notes = "注销/恢复选中班级，同时更新班级表。")
    @PostMapping(value = "/disabledCheckClazz")
    public ResultJson disabledCheckClazz(@RequestBody Long[] clazzIdList) {
        try {
            for (int i = 0; i < clazzIdList.length; i++) {
                Clazz clazz = new Clazz();
                clazz = iClazzService.getById(clazzIdList[i]);
                if (clazz.getStatus() == 0) {
                    //如果修改前为正常，修改为禁用
                    clazz.setStatus(1);
                } else if (clazz.getStatus() == 1) {
                    //如果修改前为禁用，修改为正常
                    clazz.setStatus(0);
                }
                iClazzService.updateById(clazz);
            }
            return ResultJson.success("修改成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //注销/恢复班级
    @ApiOperation(value = "注销/恢复单个班级", notes = "注销/恢复单个班级，同时更新班级表。")
    @GetMapping(value = "/disabledClazz")
    public ResultJson disabledClazz(long id) {
        try {
            Clazz clazz = new Clazz();
            clazz = iClazzService.getById(id);
            if (clazz.getStatus() == 0) {
                //如果状态为正常，修改为禁用
                clazz.setStatus(1);
            } else if (clazz.getStatus() == 1) {
                //如果状态为禁用，修改为正常
                clazz.setStatus(0);
            }
            //修改
            iClazzService.updateById(clazz);
            return ResultJson.success("操作成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }


}

